package com.lap.framework.junit.domain.generator;

import com.lap.framework.junit.domain.ClassInfo;
import com.lap.framework.junit.domain.FileInfo;
import com.lap.framework.junit.domain.valueobject.JField;
import com.lap.framework.junit.domain.valueobject.JMethod;
import com.lap.framework.junit.shared.tool.CodeType;
import com.lap.framework.junit.shared.tool.Constant;
import com.lap.framework.junit.shared.tool.ProjectUtil;
import java.util.List;
import java.util.stream.Collectors;

public class CvsGenerator implements CodeGenerator {

  @Override
  public List<FileInfo> generate(ClassInfo classInfo) {
    return classInfo.getMethods().stream()
        // .filter(JMethod::isArgMap)
        .map(
            method -> {
              String path = ProjectUtil.getCvsPath(method.getCsvPath());
              String fileName = method.getName() + ".csv";
              String content = getContent(method);
              return FileInfo.of(path, fileName, content);
            })
        .toList();
  }

  private String getContent(JMethod method) {
    StringBuilder result = new StringBuilder(256);
    String header =
        method.getFields().stream().map(JField::getDataName).collect(Collectors.joining(","));

    result.append(header);
    result.append(",").append(Constant.RESULT_CODE).append(Constant.WRAP);

    method.getFields().forEach(item -> result.append(","));
    result.append(Constant.WRAP);

    return result.toString();
  }

  @Override
  public CodeType getType() {
    return CodeType.CVS;
  }
}
